﻿using System;
using TechTalk.SpecFlow;
using Agility.TestProject.AppDriver;
using NUnit.Framework;
using Agility.TestProject.WebAii;

namespace Agility.Testing.Web.Features.StepDefinitions
{
    [Binding]
    public class CreateUserSteps
    {
        string duplicate;

        #region Background
        [Given(@"I have navigated to the registration form")]
        public void GivenIHaveNavigatedToTheRegistrationForm()
        {
            ((AgilitySilverLightDriver)ScenarioContext.Current["AppDriver"]).Initialize();
            ((AgilitySilverLightDriver)ScenarioContext.Current["AppDriver"]).OpenRegistrationForm();
        }
        #endregion

        #region RegisterUser
        [Given(@"I have entered valid information in all fields of the registration form")]
        public void GivenIHaveEnteredValidInformationInAllFieldsOfTheRegistrationForm()
        {
            ((AgilitySilverLightDriver)ScenarioContext.Current["AppDriver"]).CompleteRegistrationForm();
        }

        [When(@"I press register")]
        public void WhenIPressRegister()
        {
            ((AgilitySilverLightDriver)ScenarioContext.Current["AppDriver"]).SubmitRegistration();
        }

        [Then(@"system logs me in")]
        public void ThenSystemLogsMeIn()
        {
            Assert.IsTrue(((AgilitySilverLightDriver)ScenarioContext.Current["AppDriver"]).VerifyLoginStatus());
        }

        [Then(@"displays my screen name and login status as logged in")]
        public void ThenDisplaysMyScreenNameAndLoginStatusAsLoggedIn()
        {
            Assert.IsTrue(((AgilitySilverLightDriver)ScenarioContext.Current["AppDriver"]).VerifyDisplayName());
        }
        #endregion

        #region Duplicate Username
        [Given(@"I have entered an already existing user name")]
        public void GivenIHaveEnteredAnAlreadyExistingUserName()
        {
            duplicate = ((AgilitySilverLightDriver)ScenarioContext.Current["AppDriver"]).CreateUser();
        }

        [Given(@"populated all other required fields with valid data")]
        public void GivenPopulatedAllOtherRequiredFieldsWithValidData()
        {
            ((AgilitySilverLightDriver)ScenarioContext.Current["AppDriver"]).CompleteRegistrationForm(duplicate, Guid.NewGuid().ToString());
        }

        [Then(@"a validation error is displayed for ""(.*)""")]
        public void ThenAValidationErrorIsDisplayedFor(string p0)
        {
            Assert.IsTrue(((AgilitySilverLightDriver)ScenarioContext.Current["AppDriver"]).VerifyValidationErrorNotification(p0));
        }

        #endregion

        #region Duplicate Email
        [Given(@"I have entered an already existing email address")]
        public void GivenIHaveEnteredAnAlreadyExistingEmailAddress()
        {
            duplicate = ((AgilitySilverLightDriver)ScenarioContext.Current["AppDriver"]).CreateUser();
        }

        [Given(@"I have populated all other required fields with valid data")]
        public void GivenIHavePopulatedAllOtherRequiredFieldsWithValidData()
        {
            ((AgilitySilverLightDriver)ScenarioContext.Current["AppDriver"]).CompleteRegistrationForm(Guid.NewGuid().ToString(), duplicate);
        }

        #endregion

        #region UsernameValidation
        [Given(@"I have entered a username that is too short")]
        public void GivenIHaveEnteredAUsernameThatIsTooShort()
        {
            ((AgilitySilverLightDriver)ScenarioContext.Current["AppDriver"]).CompleteRegistrationForm("ab", Guid.NewGuid().ToString());
        }
        
        #endregion

        #region EmailValidation
        [Given(@"I have entered an invalid email address")]
        public void GivenIHaveEnteredAnInvalidEmailAddress()
        {
            ((AgilitySilverLightDriver)ScenarioContext.Current["AppDriver"]).CompleteRegistrationForm(Guid.NewGuid().ToString(), "testing@testing");
        }

        #endregion
    }
}
